home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Networking / Network Stream / main.cp next >
Encoding:
Text File  |  1996-05-28  |  3.5 KB  |  156 lines  |  [TEXT/CWIE]

  1. #include <iostream.h>
  2. #include <OpenTransport.h>
  3. #include <OpenTptInternet.h>
  4.  
  5. #include "TNetworkStream.h"
  6.  
  7. //
  8. // TNetworkSession  - OpenTransport Network Session class 
  9. //
  10. class TNetworkSession 
  11. {
  12. //     CONSTRUCTORS AND DESTRUCTORS
  13. public:
  14.      TNetworkSession();
  15.     virtual    ~TNetworkSession();
  16.  
  17. // FUNCTIONS
  18.     void     Close();
  19.     void     sendSomeData();
  20.     
  21. // ACCESSORS
  22. public:
  23.         EndpointRef        GetEndpoint()     { return fEndPoint; };
  24.   TNetworkIOStream&        GetStreamRef()     { return fioStream; };
  25.         
  26. // OPEN TRANSPORT CALLBACK
  27. private:
  28.     static pascal void NotifyProc (TNetworkSession* , OTEventCode , OTResult , void* );
  29.  
  30. // PROTECTED FIELDS
  31. protected:
  32.     EndpointRef             fEndPoint;            // Endpoint ref
  33.     TEndpointInfo            fInfo;                // Endpoint information
  34.     TNetworkIOStream        fioStream;            // ioStream object
  35. };
  36.  
  37.  
  38. // ---------------------------------------------------------------------------
  39. //     NotifyProc (queue up an event)
  40. // ---------------------------------------------------------------------------
  41. //     TNetworkSession OT Notifier Proc  
  42.  
  43. pascal void TNetworkSession::NotifyProc(void* contextPtr , OTEventCode theEvent, OTResult theResult, void* theParam)
  44. {    
  45.     TNetworkSession* theSession = contextPtr; 
  46.     try
  47.     {
  48.         switch ( theEvent ){
  49.  
  50.     ///// OpenEndpoint Completed
  51.                 case T_OPENCOMPLETE:    
  52.                     // record endpoint 
  53.                         theSession->fEndPoint = (EndpointRef) theParam;
  54.                     // Enable AckSends
  55.                         ::OTAckSends(theSession->fEndPoint);
  56.                     // attach endpoint to stream;
  57.                         theSession->fioStream.attach(theSession->fEndPoint, & theSession->fInfo);    
  58.                     // .... your code here..        
  59.                         break;
  60.                         
  61.     //// No-copy memory was released        
  62.             case T_MEMORYRELEASED:
  63.                         TNetworkBuf::Release(theParam); 
  64.                         break; 
  65.     ////..Other ...
  66.                 default:
  67.                     break;
  68.             }
  69.     }
  70.         
  71.     catch(...)        // catch everything
  72.     {
  73.                 DebugStr("\p TNetworkSession::NotifyProc -- Other Exception.. ");
  74.     } 
  75.  
  76. }
  77.  
  78. // ---------------------------------------------------------------------------
  79. //     TNetworkSession
  80. // ---------------------------------------------------------------------------
  81. //    Default Constructor
  82.  
  83. TNetworkSession::TNetworkSession() 
  84. {
  85.     
  86. // Setup a new end    point
  87.     fEndPoint = kOTInvalidEndpointRef;
  88.       ::OTAsyncOpenEndpoint( ::OTCreateConfiguration(kTCPName)  ,0, &fInfo,(*OTNotifyProcPtr) NotifyProc, this );
  89.  
  90. // more code here 
  91. }
  92.  
  93.  
  94. // ---------------------------------------------------------------------------
  95. //     TNetworkAcceptor::Close(  )
  96. // ---------------------------------------------------------------------------
  97. //    Session completed..
  98.  
  99. void TNetworkSession::Close()
  100. {
  101.     OTResult state = ::OTGetEndpointState(fEndPoint);
  102.     
  103. // If your not shutdown yet
  104.     if(state == T_DATAXFER) {
  105.     
  106. // flush out all waiting data
  107.         fioStream << flush;
  108.             
  109. // and disconnect the endpoint
  110.     if( (fInfo.flags == T_COTS_ORD) || (fInfo.flags == T_TRANS_ORD))
  111.         ::OTSndOrderlyDisconnect(fEndPoint);
  112.     else 
  113.         ::OTSndDisconnect (fEndPoint,nil);
  114.         }
  115. }
  116.  
  117.  
  118.  
  119.  
  120. // ---------------------------------------------------------------------------
  121. //     TNetworkAcceptor::sendSomeData(  )
  122. // ---------------------------------------------------------------------------
  123. //    Send some networking data
  124. #define CRLF "\r\n"
  125.  
  126. void TNetworkSession::sendSomeData()
  127. {
  128.     TNetworkIOStream&  nio   = GetStreamRef();
  129.  
  130.     nio << "HTTP/1.0 200 OK" << CRLF;
  131.     nio << "MIME-Version: 1.0" << CRLF;
  132.     nio << "Content-type: text/html" << CRLF;
  133.     nio << "Server: Knucklehead /1.0" << CRLF;
  134.     nio <<  CRLF << flush;
  135.  
  136. }
  137.  
  138.  
  139.  
  140. // 
  141. //
  142. //
  143.  
  144. void main()
  145. {
  146.     TNetworkSession *theSession;
  147.     
  148.     theSession = new TNetworkSession;
  149.     
  150.     theSession->sendSomeData();
  151.     
  152.     theSession->Close;
  153.     
  154.     delete theSession;
  155. }
  156.